package com.cci.kangdao.service.impl;

import java.util.List;
import java.util.Map;
import java.util.Timer;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.servlet.ModelAndView;

import com.cci.kangdao.dao.ActivateIOSUserPhoneTokenTDao;
import com.cci.kangdao.dao.LoginDao;
import com.cci.kangdao.dao.WorkOrderTDao;
import com.cci.kangdao.dao.model.ActivateIOSUserPhoneTokenT;
import com.cci.kangdao.dao.model.UserT;
import com.cci.kangdao.service.ActivateIOSTimerService;
import com.cci.kangdao.utilTool.AESTool;
import com.cci.kangdao.utilTool.ParameterTool;


//服务技师出发到服务完成过程中，每三分钟激活指定token的IOS手机后台app进程
@Service("ActivateIOSTimerService")
public class ActivateIOSTimerServiceImpl implements ActivateIOSTimerService{

	private Logger log = Logger.getLogger(this.getClass().getName());
	
	@Autowired
	private LoginDao loginDao;
	
	@Autowired
	private WorkOrderTDao workOrderTDao;
	
	@Autowired
	private ActivateIOSUserPhoneTokenTDao activateIOSUserPhoneTokenTDao;
	
	//通过服务技师userid获取该服务技师名下所有服务中的工单id
	@Override
	public ModelAndView getInServiceOrderListByUserId(Map<String, Object> map) {
		HttpServletResponse response = (HttpServletResponse)map.get("response");
		HttpServletRequest request = (HttpServletRequest)map.get("request");
		try{
			String userADId = AESTool.decryptString(request.getHeader("userid"));//解密userid
			UserT loginUserT = loginDao.getUserByid(userADId);
			map.put("UserId",  loginUserT.getId());
			JSONObject obj = new JSONObject();   //保存返给手机端的数据容器
			//根据服务技师userid获取这个服务技师下所有工单状态在已出发，已到达，维修中的工单
			List<Map<String, Object>> workOrderTList = workOrderTDao.getInServiceOrderListByUserId(map);
			obj.put("status", 0);
			obj.put("msg", "查询工单列表成功");
			obj.put("data", workOrderTList);	
			ParameterTool.writeResponse(response, obj.toString());	
		} catch(Exception e) {
			ParameterTool.writeErrorResponse(response);	
			//主动回滚事务
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			log.error(e.getMessage(),e);
		}
		return null;
	}
	
	

	//服务技师userName和手机token绑定
	@Transactional
	@Override
	public ModelAndView userNameBindingToToken(Map<String, Object> map) {
		HttpServletResponse response = (HttpServletResponse)map.get("response");
		HttpServletRequest request = (HttpServletRequest)map.get("request");
		try{
			String userADId = AESTool.decryptString(request.getHeader("userid"));//解密userid
			String userName = loginDao.getUserByid(userADId).getUserName();  //获取登录的服务技师用户名
			String phoneToken = (String)map.get("IOStoken");
			JSONObject obj = new JSONObject();   //保存返给手机端的数据容器
			//先判断数据库中是否有这个服务技师用户名对应的IOStoken
			ActivateIOSUserPhoneTokenT activateIOSUserPhoneTokenT = activateIOSUserPhoneTokenTDao.getActivateIOSUserPhoneTokenTByUserName(userName);
			if(activateIOSUserPhoneTokenT != null) {  //如果有，就更新这个服务技师登录名对应的IOStoken
				activateIOSUserPhoneTokenTDao.updateActivateIOSUserPhoneTokenT(userName, phoneToken);
			}else {  //没有则插入一条用户名和IOStoken绑定的信息
				activateIOSUserPhoneTokenTDao.insertActivateIOSUserPhoneTokenT(userName, phoneToken);
			}
			obj.put("status", 0);
			obj.put("msg", "绑定成功!当前服务技师用户名为"+userName +"，手机token为"+phoneToken);
			obj.put("data", "");	
			ParameterTool.writeResponse(response, obj.toString());	
		} catch(Exception e) {
			ParameterTool.writeErrorResponse(response);	
			//主动回滚事务
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			log.error(e.getMessage(),e);
		}
		return null;
	}

	
	//服务技师用户名和token解绑
	@Transactional
	@Override
	public ModelAndView userNameUnBindingToToken(Map<String, Object> map) {
		HttpServletResponse response = (HttpServletResponse)map.get("response");
		HttpServletRequest request = (HttpServletRequest)map.get("request");
		try{
			String userADId = AESTool.decryptString(request.getHeader("userid"));//解密userid
			String userName = loginDao.getUserByid(userADId).getUserName();  //获取登录的服务技师用户名
			JSONObject obj = new JSONObject();   //保存返给手机端的数据容器
			activateIOSUserPhoneTokenTDao.deleteActivateIOSUserPhoneTokenT(userName);  //删除关于登录服务技师用户名绑定的token信息
			obj.put("status", 0);
			obj.put("msg", "解绑成功!");
			obj.put("data", "");	
			ParameterTool.writeResponse(response, obj.toString());	
		} catch(Exception e) {
			ParameterTool.writeErrorResponse(response);	
			//主动回滚事务
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			log.error(e.getMessage(),e);
		}
		return null;
	}
}
